package Chepter9;

import java.util.Scanner;

public class Chepter9_9_9 {
	
	static void printMenu(){
		System.out.println("\nMain menu");
		System.out.println("1: addStudent");
		System.out.println("2: getStudents");
		System.out.println("3: getNumberOfStudents");
		System.out.println("4: getCourseName");
		System.out.println("5: dropStudent");
		System.out.println("6: clear");
	}
	
	public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		System.out.print("Input Course : ");
		String cor  = input.next();
		Course course = new Course(cor);
		int menu;
		String[] temp;
		
		while(true){
			printMenu();
			System.out.print("Enter a choice : ");
			menu = input.nextInt();
			
			switch(menu){
			case 1 : 
				System.out.print("Enter a Student : ");
				course.addStudent(input.next());
				break;
			case 2 : 
				temp = course.getStudents();
				for(int i = 0 ; i < course.getNumberOfStudents() ; i ++){
					System.out.print(temp[i] + "\t");
				}
				break;
			case 3 : 
				System.out.print("NumberOfStudents = " + course.getNumberOfStudents() + "\n");
				break;
			case 4 : 
				System.out.print("CourseName = " + course.getCourseName() + "\n");
				break;
			case 5 : 
				System.out.print("Enter a Student : ");
				course.dropStudent(input.next());
				break;
			case 6 :
				course.clear();
				break;
			}
		}
	}
	
}

class Course {
	  private int MAX = 2;
	  private String courseName;
	  private String[] students = new String[MAX];
	  private int numberOfStudents;
	  
	    
	  public Course(String courseName) {
	    this.courseName = courseName;
	  }
	  
	  public void addStudent(String student) {
		  if( numberOfStudents == (MAX-1)){
			  String[] temp = new String[students.length+1];
			  System.arraycopy(students, 0, temp, 0, students.length);
			  students = temp;
			  students[numberOfStudents] = student;
			  numberOfStudents++;
			  MAX++;
		  }else{
			  students[numberOfStudents] = student;
			  numberOfStudents++;
		  }
	    
	  }
	  
	  public String[] getStudents() {
	    return students;
	  }
	  
	  public int getNumberOfStudents() {
	    return numberOfStudents;
	  }  

	  public String getCourseName() {
	    return courseName;
	  }  
	  
	  public void dropStudent(String student) {
	    // Left as an exercise in Exercise 9.9
		boolean check = false;
    	for(int i = 0 ; i <= numberOfStudents-1 ; i ++){
			if(students[i].equals(student)){
				check = true;
			}
		}  
		if(check){
			if(numberOfStudents == 1){
				numberOfStudents = 0;
			}else if(numberOfStudents == 2){
				if(students[0].equals(student)){
					students[0] = students[1];
					numberOfStudents--;
				}else{
					numberOfStudents--;
				}
			}else{
				for(int i = 0 ; i <= numberOfStudents-2 ; i ++){
					if(students[i].equals(student)){
						for(int j = i ; j <= numberOfStudents-2 ; j ++){
							students[j] = students[j+1];
							numberOfStudents--;
						}
					}
				}
			}
		}else{
			System.out.print("Can't find student\n");
		}
		
		  
		  
		
	  }
	  
	  public void clear() {
		numberOfStudents = 0;
	  }
}